package common

import (
	"fmt"
	"golang.org/x/crypto/bcrypt"
)

// HashAndSalt 加密密码
func HashAndSalt(pwd []byte) string {
	if hash, err := bcrypt.GenerateFromPassword(pwd, bcrypt.MinCost);err != nil {
		fmt.Println("加密失败")
		return ""
	}else{
		return string(hash)
	}
}

// ValidatePasswords 验证密码
func ValidatePasswords(hashedPwd string, plainPwd []byte) bool {
	byteHash := []byte(hashedPwd)
	err := bcrypt.CompareHashAndPassword(byteHash, plainPwd)
	if err != nil {
		return false
	}
	return true
}